package com.example.androidproject.net;


import com.example.androidproject.BuildConfig;
import com.example.androidproject.data.bean.UserInfoTools;
import com.example.androidproject.data.response.LoginResponse;
import com.hyg.idea.net.common.IdeaApiProxy;
import com.hyg.idea.net.token.IGlobalManager;
import com.hyg.idea.net.token.RefreshTokenResponse;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;


public class RetrofitHelper {

    private static IdeaApiService mIdeaApiService;

    public static IdeaApiService getApiService() {
        if (mIdeaApiService == null)
            mIdeaApiService = new IdeaApiProxy().getApiService(IdeaApiService.class,
                    BuildConfig.BASE_URL, new IGlobalManager() {
                        @Override
                        public void logout() {
                            UserInfoTools.logout();
                        }

                        @Override
                        public void tokenRefresh(RefreshTokenResponse response) {
                          UserInfoTools.updateToken(response);
                        }
                    });
        return mIdeaApiService;
    }

    public static Observable<LoginResponse> login(String username, String password){
        return getApiService().login(username, password);
    }

    public static Observable<String> authResult(String str){
        Map<String,Object> params = new HashMap<>();
        params.put("key",str);
        return getApiService().authResult(RequestBodyUtils.jsonBody(params));
    }
}
